#! /usr/bin/env python
# -*- coding: utf-8 -*-

# *************************************************************
#  Copyright (c) Huoty - All rights reserved
#
#      Author: Huoty <sudohuoty@gmail.com>
#  CreateTime: 2017-03-31 16:17:25
# *************************************************************

from __future__ import print_function

import math

def redis_section_lrange(key, page=None, count=500, reverse=False):
    """分段查询 ssdb list 数据

    参数：
        key: 查询的键
        page: 要查询的数据段，为假值时一次返回所有的数据
        count: 如果 page 为真值时表示每段返回的总条数，否则返回开头的 count 条数据
        reverse: 是否逆序查询

    返回：json 反序列化后的 list 数据
    """
    llen = 218
    if llen == 0:
        return []

    if page:
        step = count if count else 500
        max_page = math.ceil(llen/float(step))
        if page < 0 or page > max_page:
            return []

        if reverse:
            start = llen - (step * page) if page < max_page else 0
            end = llen - (step * (page - 1)) - 1
        else:
            start = step * (page - 1)
            end = step * page - 1
    else:
        start = 0
        end = count if count else -1

    return start, end

# Script starts from here

if __name__ == "__main__":
    print("page:1, reverse: false, results", redis_section_lrange(None, page=1, count=50, reverse=False))
    print("page:2, reverse: false, results", redis_section_lrange(None, page=2, count=50, reverse=False))
    print("page:3, reverse: false, results", redis_section_lrange(None, page=3, count=50, reverse=False))
    print("page:4, reverse: false, results", redis_section_lrange(None, page=4, count=50, reverse=False))
    print("page:5, reverse: false, results", redis_section_lrange(None, page=5, count=50, reverse=False))
    print("page:6, reverse: false, results", redis_section_lrange(None, page=6, count=50, reverse=False))
    print("page:-1, reverse: false, results", redis_section_lrange(None, page=-1, count=50, reverse=False))
    print("page:-2, reverse: True, results", redis_section_lrange(None, page=-2, count=50, reverse=True))
    print("page:1, reverse: True, results", redis_section_lrange(None, page=1, count=50, reverse=True))
    print("page:2, reverse: True, results", redis_section_lrange(None, page=2, count=50, reverse=True))
    print("page:3, reverse: True, results", redis_section_lrange(None, page=3, count=50, reverse=True))
    print("page:4, reverse: True, results", redis_section_lrange(None, page=4, count=50, reverse=True))
    print("page:5, reverse: True, results", redis_section_lrange(None, page=5, count=50, reverse=True))
    print("page:6, reverse: True, results", redis_section_lrange(None, page=6, count=50, reverse=True))
    print("page:None, reverse: True, results", redis_section_lrange(None, page=None, count=50, reverse=True))
    print("page:None, reverse: False, results", redis_section_lrange(None, page=None, count=50, reverse=False))
    print("page:None, reverse: True, results", redis_section_lrange(None, page=None, reverse=True))
